import { compose } from '@/utils/utils'

function priceTransform(itemData) {
  let total = {
    originalPrice: 0,
    discountPrice: 0,
    petCardAmount: 0,
    deductionAmount: 0,
    paidInPrice: 0,
    receivablePrice: 0,
    payAmount: 0,
    owingPrice: 0,
    preOwingPrice: 0,
    thisPay: 0,
    paidInAmount: 0,
    prePaidInAmount: 0,
  }

  itemData.yiyaMainClosingEntries.thisPay = (+itemData.yiyaMainClosingEntries
    .thisPay).toFixed(2)

  const yiyaItemBillingExtends = itemData.yiyaItemBillingExtends.map((item) => {
    const {
      unitPrice,
      count,
      discount,
      deductionPrice: deductionAmount,
      storedDeductPrice: petCardAmount,
      projectReceived: payAmount,
      thisPay,
    } = item
    const originalPrice = +unitPrice * +count
    const discountPrice = (+originalPrice * +discount) / 100
    const paidInPrice = +discountPrice - +deductionAmount
    const receivablePrice = +paidInPrice - +petCardAmount
    const owingPrice = +receivablePrice - +payAmount
    const preOwingPrice = +owingPrice - +thisPay
    const paidInAmount = +payAmount + +petCardAmount
    const prePaidInAmount = +paidInAmount + +thisPay

    total.originalPrice += originalPrice
    total.discountPrice += discountPrice
    total.petCardAmount += petCardAmount
    total.deductionAmount += deductionAmount
    total.paidInPrice += paidInPrice
    total.receivablePrice += receivablePrice
    total.payAmount += payAmount
    total.owingPrice += owingPrice
    total.preOwingPrice += +preOwingPrice
    total.thisPay += +thisPay
    total.paidInAmount += paidInAmount
    total.prePaidInAmount += prePaidInAmount

    return {
      ...item,
      unitPrice: unitPrice.toFixed(2),
      originalPrice: originalPrice.toFixed(2),
      discountPrice: discountPrice.toFixed(2),
      petCardAmount: petCardAmount.toFixed(2),
      deductionAmount: deductionAmount.toFixed(2),
      paidInPrice: paidInPrice.toFixed(2),
      receivablePrice: receivablePrice.toFixed(2),
      payAmount: payAmount.toFixed(2),
      owingPrice: owingPrice.toFixed(2),
      preOwingPrice: preOwingPrice.toFixed(2),
      thisPay: (+thisPay).toFixed(2),
      paidInAmount: paidInAmount.toFixed(2),
      prePaidInAmount: prePaidInAmount.toFixed(2),
    }
  })

  total = {
    originalPrice: total.originalPrice.toFixed(2),
    discount: Math.floor((total.discountPrice / total.originalPrice) * 100),
    discountPrice: total.discountPrice.toFixed(2),
    petCardAmount: total.petCardAmount.toFixed(2),
    deductionAmount: total.deductionAmount.toFixed(2),
    paidInPrice: total.paidInPrice.toFixed(2),
    receivablePrice: total.receivablePrice.toFixed(2),
    payAmount: total.payAmount.toFixed(2),
    owingPrice: total.owingPrice.toFixed(2),
    preOwingPrice: total.preOwingPrice.toFixed(2),
    thisPay: total.thisPay.toFixed(2),
    paidInAmount: total.paidInAmount.toFixed(2),
    prePaidInAmount: total.prePaidInAmount.toFixed(2),
  }

  return {
    ...itemData,
    yiyaItemBillingExtends,
    total,
  }
}

/**
 * 合并项目医生
 *
 * @param itemData
 * @returns {{doctor: any[]}}
 */
function mergeDoctor(itemData) {
  return {
    ...itemData,
    doctor: [
      ...new Set(itemData.yiyaItemBillingExtends.map((item) => item.doctor)),
    ].join(','),
  }
}

/**
 * 合并欠费项目
 *
 * @param itemData
 * @returns {{owingProject: any[]}}
 */
function mergeOwingProject(itemData) {
  return {
    ...itemData,
    owingProject: itemData.yiyaItemBillingExtends
      .map((item) => (item.status === 2 ? item.itemName : ''))
      .filter((i) => i)
      .join(','),
  }
}

/**
 * 合并支付方式
 *
 * @param itemData
 * @returns {{payMethods: *}}
 */
function mergePayMethods(itemData) {
  return {
    ...itemData,
    payMethods: itemData.yiyaClosingMethods
      ? itemData.yiyaClosingMethods.map((item) => item.closingMode).join(',')
      : '',
  }
}

/**
 * 将服务端数据转换成视图数据
 * @param listData
 * @returns {{dataList: *, totalMap: Map<any, any>}}
 */
export function billDataTransform(listData) {
  const totalMap = new Map()
  const dataList = listData.map(compose(mergeDoctor, priceTransform))
  dataList.forEach((data) =>
    totalMap.set(data.yiyaItemBillingExtends, data.total)
  )
  return {
    dataList,
    totalMap,
  }
}

/**
 * 开单记录数据转换
 *
 * @param listData
 * @returns {*}
 */
export function billingRecordDataTransform(listData) {
  return listData.map((data) => {
    const mainClosingEntriesVo = compose(
      mergeDoctor,
      priceTransform
    )(data.mainClosingEntriesVo)
    const listMainClosingEntriesVos = data.listMainClosingEntriesVos.map(
      compose(mergePayMethods, mergeDoctor, priceTransform)
    )
    return {
      ...data,
      mainClosingEntriesVo,
      listMainClosingEntriesVos,
    }
  })
}

export function chargeArrearDataTransform(listData) {
  return listData.map((data) => {
    const mainClosingEntriesVo = compose(
      mergeOwingProject,
      mergeDoctor,
      priceTransform
    )(data.mainClosingEntriesVo)
    const listMainClosingEntriesVos = data.listMainClosingEntriesVos.map(
      compose(mergeOwingProject, mergeDoctor, priceTransform)
    )
    return {
      ...data,
      mainClosingEntriesVo,
      listMainClosingEntriesVos,
    }
  })
}
